﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LostLib.IO
{
	public class TempFile : IDisposable, IEquatable<TempFile>
	{
		public TempFile()
		{
			Path = System.IO.Path.GetTempFileName();
		}

		public readonly string Path;

		#region IDisposable Members

		public void Dispose()
		{
			File.Delete(Path);

			GC.SuppressFinalize(this);
		}
		~TempFile()
		{
			Dispose();
		}
		#endregion

		public override string ToString()
		{
			return Path;
		}

		public bool Equals(TempFile other)
		{
			if (other == null) return false;
			else return System.IO.Path.GetFullPath(Path).Equals(System.IO.Path.GetFullPath(other.Path));
		}

		public override bool Equals(object obj)
		{
			return Equals(obj as TempFile);
		}

		public override int GetHashCode()
		{
			return Path.GetHashCode() ^ 3567;
		}

		public static implicit operator string(TempFile file)
		{
			if (file == null)
				throw new ArgumentNullException("file");

			return file.Path;
		}
	}
}
